class OrderBackup < ActiveRecord::Base
  belongs_to :order
  
  belongs_to :customer
  belongs_to :customer_backup
  belongs_to :user
  #has_one :customer
  has_many :tickets
  belongs_to :creator, :class_name => 'User', :foreign_key => 'creator_id' #预定
  belongs_to :canceler, :class_name => 'User', :foreign_key => 'canceler_id' #取消
  belongs_to :printer, :class_name => 'User', :foreign_key => 'printer_id' #确认
  belongs_to :ticket_outer, :class_name => 'User', :foreign_key => 'ticket_outer_id' #出票
  belongs_to :cash_checker, :class_name => 'User', :foreign_key => 'cash_checker_id' #收款
  belongs_to :sender, :class_name => 'User', :foreign_key => 'sender_id' #送票
  
  belongs_to :jj_man, :class_name => 'User', :foreign_key => 'jj_man_id' #接机人
  belongs_to :jj_affirmer, :class_name => 'User', :foreign_key => 'jj_affirm_id' #接机确认人
  belongs_to :last_editer, :class_name => 'User', :foreign_key => 'last_editer_id' #最后编辑人
  
  def self.saveBK(order)
    begin
      orderBK = OrderBackup.new
      order.attributes.each do |attr, value|
        eval("orderBK.#{attr} = order.#{attr}") if attr!="id" && attr!="updated_at" && attr!="created_at"
      end
      orderBK.id = nil
      orderBK.order_id = order.id
      orderBK.updated_at = Time.new
      orderBK.created_at = Time.new
      #orderBK.last_editer_id = uid
      if orderBK.save
        return orderBK.id
      else
        return nil
      end
    rescue Exception => e
      logger.error("model OrderBackup saveBK e="+e.message)
    end
  end
end
